<template>
  <div class="model-container">
    <div class="listName">公告管理</div>

    <el-card style="margin-top: 10px">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-left">
          <el-button type="primary" plain @click="add">添加</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据" @selection-change="changeFun">

          <el-table-column align="center" width="55" label="序号" type="index" />

          <el-table-column align="center" prop="title" label="标题" />

          <el-table-column align="center" label="置顶图">
            <template slot-scope="scope">
              <img :src="scope.row.img" width="50px" height="50px">
            </template>
          </el-table-column>

          <el-table-column align="center" prop="createTime" label="创建时间" />

          <el-table-column align="center" label="操作" width="500px">
            <template slot-scope="scope">
              <el-button v-if="scope.row.topStatus === 0" type="primary" size="mini" plain @click="top(scope.row)">置顶</el-button>
              <el-button v-else type="primary" size="mini" plain @click="top(scope.row)">取消置顶</el-button>
              <el-button type="warning" size="mini" plain @click="edit(scope.row)">修改</el-button>
              <el-button type="danger" size="mini" plain @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogState" :before-close="handleClose" top="5vh" width="80%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="标题" prop="title">
            <el-input v-model="dialogBindData.title" size="small" type="text" placeholder="请输入公告标题(20字以内)" />
          </el-form-item>
          <el-form-item label="顶部图" prop="img" class="three">
            <dbt v-if="isInit" :img-logo-url="dialogBindData.img" :status="dialogType" @editSuccess="imgData" />
          </el-form-item>
          <el-form-item label="" prop="content" class="three">
            <Tinymce v-if="isInit" v-model="dialogBindData.content" />
          </el-form-item>
          <div class="btns-wrapper">
            <el-button size="small" @click="handleClose">取 消</el-button>
            <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
          </div>
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>
<script>
import * as announcement from '../../api/announcement.js'
import dbt from '../../views/announcement/component/commodity_img'
import Tinymce from '../../components/Tinymce'
export default {
  components: {
    dbt,
    Tinymce
  },
  data() {
    return {
      isInit: true,
      // 输入禁用
      disableIinput: false,
      // 列表数据
      dataOption: null,
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数

      photoGallery: [],
      type: '',
      dialogState: false,
      editorData: null,
      dialogType: 1, // 1--增加  2--编辑  3--查看
      // 对话框表单数据对象
      dialogBindData: {
        id: '',
        title: '',
        img: '',
        content: '',
        createTime: ''
      },
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }],
        img: [{
          required: true,
          message: '请上传顶部图',
          trigger: 'blur'
        }]
      },
      // 按钮状态
      buttonState: false,
      delBtn: true,
      delIdArr: []
    }
  },
  computed: {
    // 计算属性返回对话框文本
    dialogTitle() {
      if (this.dialogType === 1) {
        return '新增公告'
      }
      if (this.dialogType === 2) {
        return '公告编辑'
      } else {
        return '查看公告'
      }
    }
  },
  beforeMount() {
    // 	  this.type = this.$route.params.type // 类型
    // 	  this.editorData = this.$route.params.data // 编辑数据

    this.getList()
  },

  methods: {
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    // 删除
    changeFun(data) {
      this.datavalID = data
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        if (key === 'sellerType') {
          this.dialogBindData[key] = 1
        } else if (key === 'settlementPeriod') {
          this.dialogBindData[key] = 1
        } else {
          this.dialogBindData[key] = ''
        }
      }
      this.dialogState = false
      this.disabled = false
      this.disableIinput = false
      this.isInit = false

      // this.$refs.dialogBindData.clearValidate()
    },
    // 查询列表数据
    getList() {
      const param = {
        current: this.pageNo,
        size: this.pageSize
      }
      announcement.getNotify(param).then(res => {
        console.log('查询公告列表的响应', res)
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 添加
    add() {
      this.dialogType = 1
      this.dialogState = true
      this.dialogBindData.content = ''
      this.isInit = true
    },
    // 置顶
    top(data) {
      if (data === 0) {
        if (this.datavalID.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择商家!'
          })
        }
        return false
      }
      let str
      if (data.topStatus === 0) {
        str = '确认要置顶吗,是否继续?'
        this.$confirm(str, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.topDataTable(data)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      } else {
        str = '确认要取消置顶吗,是否继续?'
        this.$confirm(str, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.untopDataTable(data)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      }
    },

    // 取消置顶
    untopDataTable(data) {
      const param = {
        id: data.id,
        topStatus: data.topStatus === 0 ? 1 : 0
      }

      announcement.topNotify(param).then(res => {
        if (res.data.code !== 1) {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
          this.buttonState = false
          return false
        }
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.buttonState = false
        this.getList()
        this.handleClose()
      })
    },

    // 置顶公告
    topDataTable(data) {
      const param = {
        id: data.id
        // topStatus: data.topStatus === 0 ? 1 : 0
      }

      announcement.topNotifyNew(param).then(res => {
        if (res.data.code !== 1) {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
          this.buttonState = false
          return false
        }
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.buttonState = false
        this.getList()
        this.handleClose()
      })
    },

    // 编辑
    edit(data) {
      this.dialogType = 2
      this.dialogState = true
      this.disabled = true
      this.isInit = true
      this.dialogBindData.id = data.id
      this.reBindData(data)
    },
    // 删除
    del(data) {
      if (data === 0) {
        if (this.datavalID.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择商家!'
          })
        }
        return false
      }
      this.$confirm('确认要删除公告吗,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delDataTable(data)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    delDataTable(data) {
      const ids = []
      ids.push(data.id)
      announcement.deleteNotify(ids).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }
      })
    },
    // 提交
    submmitFun() {
      const param = {
        title: this.dialogBindData.title,
        img: this.dialogBindData.img,
        content: this.dialogBindData.content
      }

      if (this.dialogType !== 1) {
        param.id = this.dialogBindData.id
      }

      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            announcement.addNotify(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          } else {
            announcement.updateNotify(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          }
        } else {
          return false
        }
      })
    },
    // 接收
    imgData(data) {
      this.dialogBindData.img = data
    }

  }
}
</script>
<style>

</style>
